﻿// -----------------------------------------------------------------------
// <copyright file="Flyer.cs" company="Diddly Squad">
// </copyright>
// -----------------------------------------------------------------------

using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DiddlySquat
{
	/// <summary>
	/// Flying enemy class.
	/// </summary>
	public class Flyer : Enemy
	{
		public Flyer()
		{
			this.EnemySprite = Constants.FlyerSprite;
			this.SizeFactor = Constants.FlyerSizeFactor;
			this.EnemyRestitution = Constants.FlyerRestitution;
			this.EnemyFriction = Constants.FlyerFriction;
            this.Damage = Constants.FlyerDamage;
			this.EnemyType = Enums.EnemyType.Flyer;
            this.IsFlying = Constants.FlyerIsFlying;
            this.Speed = Constants.FlyerSpeed;
            this.MaxSpeed = Constants.FlyerMaxSpeed;
            this.Range = Constants.FlyerRange;
		}

		/// <summary>
		/// Creates flying enemy to the world.
		/// </summary>
		/// <param name="world">The world.</param>
		/// <param name="position">The position.</param>
		/// <param name="body">The body.</param>
		/// <param name="sprite">The sprite.</param>
		/// <param name="health">The health.</param>
		/// <param name="deathSprite">The death sprite.</param>
        public override void Initialize(World world, Vector2 position, Body body, Texture2D sprite, double health, Texture2D deathSprite)
        {
            base.Initialize(world, position, body, sprite, health, deathSprite);
            this.Body.IgnoreGravity = true;          
        }

		/// <summary>
		/// Creates flying enemy to the world.
		/// </summary>
		/// <param name="world">The world.</param>
		/// <param name="position">The position.</param>
		/// <param name="body">The body.</param>
		/// <param name="sprite">The sprite.</param>
		/// <param name="health">The health.</param>
		/// <param name="weapon">The weapon.</param>
		/// <param name="deathSprite">The death sprite.</param>
        public override void Initialize(World world, Vector2 position, Body body, Texture2D sprite, double health, Weapon weapon, Texture2D deathSprite)
        {
            base.Initialize(world, position, body, sprite, health, weapon, deathSprite);
            this.Body.IgnoreGravity = true;
        }

		/// <summary>
		/// Creates flying enemy to the world.
		/// </summary>
		/// <param name="world">The world.</param>
		/// <param name="position">The position.</param>
		/// <param name="body">The body.</param>
		/// <param name="sprite">The sprite.</param>
		/// <param name="health">The health.</param>
        public override void Initialize(World world, Vector2 position, Body body, Texture2D sprite, double health)
        {
            base.Initialize(world, position, body, sprite, health);
            this.Body.IgnoreGravity = true;
        }

		/// <summary>
		/// Updates the enemy and moves towards target.
		/// </summary>
		/// <param name="gameTime">The game time.</param>
		/// <param name="target">The target.</param>
        public override void Update(GameTime gameTime, Character target)
        {
            if (gameTime.TotalGameTime.Milliseconds % 501 == 0)
                Move(target);

            base.Update(gameTime);
        }

		/// <summary>
		/// Moves the enemy towards specified target.
		/// </summary>
		/// <param name="target">The target.</param>
        public override void Move(Character target)
        {
                // The enemy flies with a constant speed towards the player
                if (this.DistanceToTarget(target) < this.Range)
                {
                    this.Body.LinearVelocity = new Vector2(((target.Position.X - this.Position.X) / this.DistanceToTarget(target)), ((target.Position.Y - this.Position.Y) / this.DistanceToTarget(target))) * this.Speed;
                }
                if (this.DistanceToTarget(target) >= this.Range)
                {
                    this.Body.LinearVelocity = new Vector2(0, 0);
                }
        }
	}
}